package com.bw.solr;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;

/**
 * 添加修改、删除、查询
 * @author liang
 *
 */
public class SolrTest {

	public static void main(String[] args) throws SolrServerException,
			IOException {

//		updateTest();
		queryTest();
//		deleteTest();
//		queryTest();
//		queryTest();
	}

	public static void updateTest() throws SolrServerException, IOException {
		
		String solrUrl = "http://localhost:28080/solr/mycore";

		HttpSolrClient client = new HttpSolrClient(solrUrl);

		SolrInputDocument doc = new SolrInputDocument();
		doc.addField("id", "renyuan3");//UUID.randomUUID().toString().replace("-", ""));
//		doc.addField("title","昨天月亮很红");
//		doc.addField("text_ik", "晚上大概8点的时候，发生月食，月亮很红。");
//		doc.addField("title","美国看不到");
//		doc.addField("text_ik", "晚上大概8点的时候，发生月食，中国月亮很红，美国没有。");
		doc.addField("name", "大一3");
//		doc.addField("age", "40");
//		doc.addField("height", "170");
//		doc.addField("last_modified", new Date());
//		client.deleteByQuery("*:*");
		client.add(doc);
//		client.commit();

		SolrInputDocument doc1 = new SolrInputDocument();
//		doc1.addField("id", "1511_A1");//UUID.randomUUID().toString().replace("-", ""));
//		doc1.addField("title","耐克2");
		doc1.addField("id", "1511_news2");
		doc1.addField("title","美国看不到");
//		doc1.addField("text_ik", "晚上大概8点的时候，发生月食，中国月亮很红，美国没有。");
//		doc.addField("title", "大一");
//		doc.addField("age", "40");
//		doc.addField("height", "170");
//		client.deleteByQuery("*:*");
		client.add(doc1);
		client.commit();

	}
	public static void deleteTest() throws SolrServerException, IOException {

		String solrUrl = "http://localhost:28080/solr/mycore";
		HttpSolrClient client = new HttpSolrClient(solrUrl);
		UpdateResponse res ;
//		res = client.deleteByQuery("*:*");
//		res = client.deleteByQuery("id:1511_A1");
//		res = client
//				.deleteById("d89eb78c65824d7190d7a083e5ad4811");
		res = client.deleteById("1511_news2");
		client.commit();
		System.out.println("删除结果："+res.getStatus());
	}


	public static void queryTest() throws SolrServerException, IOException {

		String solrUrl = "http://localhost:38080/solr/mycore";
		HttpSolrClient client = new HttpSolrClient(solrUrl);
		
		System.out.println(ClientUtils.escapeQueryChars("-"));

		SolrQuery q = new SolrQuery();
		q.set("q", "*:*");
//		q.set("q", "title:耐克");
		q.setStart(0);
		q.setRows(3);
//		q.setParam("fl", "student_name");//结果显示那些字段
		
		//q.setFilterQueries("+student_name:北京");
		System.out.println(q.toString());
		QueryResponse res = client.query(q);
		
		SolrDocumentList results = res.getResults();
		System.out.println("总条数："+results.getNumFound());
		for (SolrDocument doc : results) {
			System.out.println(doc);
		}
	}

}
